package com.boot.util;

import com.boot.app.base.domain.BaseUser;
import com.boot.app.base.dto.UserDetailsImpl;
import com.boot.exception.UnAuthenticationException;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.context.SecurityContextHolder;

@Configuration
public class SessionUtil {

    private UserDetailsImpl getUserDetailsImpl(){
        try{
            return (UserDetailsImpl) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        }catch (ClassCastException e){
            throw new UnAuthenticationException();
        }
    }

    public long getUserId(){
        return getUser().getId();
    }

    public String getUsername(){
        return getUserDetailsImpl().getUsername();
    }

    public BaseUser getUser(){
        return getUserDetailsImpl().getUser();
    }

}
